﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System.Windows.Forms;
    using ConnectBasic.Shell.Extensibility;
    using System.Drawing;
    using System;

    internal partial class EditorZone : UserControl, IEditorService
    {
        private IEditor mActiveEditor;
        private EncapsulatedServiceProvider mActiveEditorServiceProvider = new EncapsulatedServiceProvider();

        public EditorZone()
        {
            InitializeComponent();
            this.UpdateState();
        }

        #region IEditorService Members

        public bool ActivateEditor(IEditor editor)
        {
            TabPage page = null;

            // See if the editor is already shown
            foreach (TabPage editorPage in EditorTabs.TabPages)
            {
                if (editorPage.Tag == editor)
                {
                    page = editorPage;
                    break;
                }
            }

            // If not create a new one
            if (page == null)
            {
                var editControl = editor.GetEditControl();
                editControl.Dock = DockStyle.Fill;

                page = new TabPage(editor.Title);
                page.Tag = editor;
                page.Controls.Add(editControl);

                EditorTabs.TabPages.Add(page);
            }

            // Show it
            EditorTabs.SelectTab(page);

            // Make sure all our state is up to date
            this.UpdateState();

            // Success 
            return true;
        }

        public string ActiveEditorFileName
        {
            get
            {
                if (EditorTabs.TabCount > 0)
                {
                    return ((IEditor)EditorTabs.SelectedTab.Tag).FileName;
                }
                else
                {
                    return null;
                }
            }
        }

        public IServiceProvider ActiveEditorServiceProvider
        {
            get
            {
                return mActiveEditorServiceProvider;
            }
        }

        #endregion

        #region Helpers

        private void UpdateState()
        {
            if (EditorTabs.TabCount == 0)
            {
                mActiveEditor = null;
                mActiveEditorServiceProvider.Implementation = null;

                this.BorderStyle = BorderStyle.FixedSingle;
                this.BackColor = SystemColors.AppWorkspace;
                this.EditorTabs.Visible = false;
            }
            else
            {
                mActiveEditor = EditorTabs.SelectedTab.Tag as IEditor;
                mActiveEditorServiceProvider.Implementation = mActiveEditor;

                this.BorderStyle = BorderStyle.None;
                this.BackColor = SystemColors.Control;
                this.EditorTabs.Visible = true;
            }
        }

        #endregion

    }
}
